package com.wordplat.quickstart.slice;

import com.wordplat.ikvstockchart.InteractiveKLineView;
import com.wordplat.ikvstockchart.entry.Entry;
import com.wordplat.ikvstockchart.entry.EntrySet;
import com.wordplat.ikvstockchart.render.TimeLineRender;
import com.wordplat.quickstart.ResourceTable;
import com.wordplat.quickstart.base.BaseAbilitySlice;
import com.wordplat.quickstart.bean.BtcBean;
import com.wordplat.quickstart.mvp.BtcChinaPresenter;
import com.wordplat.quickstart.mvp.LoadingViewListener;

import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * Simple_TimeLine_Example_Slice
 *
 * @since 2021-04-30
 */
public class Simple_TimeLine_Example_Slice extends BaseAbilitySlice {
    private static final int REQUEST_BTC_DATA = 1;
    private static final int NUM_0 = 0;
    private static final int NUM_2 = 2;
    private static final int NUM_4 = 4;
    private final HiLogLabel label = new HiLogLabel(0, 0, "Simple_TimeLine_Example_Slice");
    private InteractiveKLineView timeLineView = null;
    private final LoadingViewListener btcChinaListener = new LoadingViewListener() {
        @Override
        public void onStartRequest(int requestCode) {
            debug("onStartRequest");
        }

        @Override
        public void onFinishRequest(int requestCode) {
            debug("onFinishRequest");
        }

        @Override
        public void onSuccess(int requestCode) {
            debug("onSuccess");
            for (BtcBean btcBean : presenter.getBtcList()) {
                Entry entry = new Entry(btcBean.getPrice(), (int) btcBean.getAmount(), "");
                entrySet.addEntry(entry);
            }
            entrySet.getEntryList().get(NUM_0).setXLabel("09:30");
            entrySet.getEntryList().get(NUM_2).setXLabel("11:30/13:00");
            entrySet.getEntryList().get(NUM_4).setXLabel("15:00");

            timeLineView.notifyDataSetChanged();
        }

        @Override
        public void onResultEmpty(int requestCode) {
            entrySet.setLoadingStatus(false);
            timeLineView.notifyDataSetChanged();
        }

        @Override
        public void onNoNetworkError(int requestCode) {
            super.onNoNetworkError(requestCode);
            debug("onNoNetworkError");
            onResultEmpty(requestCode);
        }

        @Override
        public void onNetworkTimeOutError(int requestCode) {
            super.onNetworkTimeOutError(requestCode);
            debug("onNetworkTimeOutError");
            onResultEmpty(requestCode);
        }
    };

    private final EntrySet entrySet = new EntrySet();

    private final BtcChinaPresenter presenter = new BtcChinaPresenter();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_simple_timeline);
        initViews();
    }

    /**
     * 初始化View
     */
    private void initViews() {
        timeLineView = (InteractiveKLineView) findComponentById(ResourceTable.Id_timeLineView);
        timeLineView.setEntrySet(entrySet);
        timeLineView.setRender(new TimeLineRender());
    }

    @Override
    protected void onActive() {
        super.onActive();
        presenter.attachView(btcChinaListener);
        presenter.getSimple(REQUEST_BTC_DATA);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        presenter.detachView();
    }

    private void debug(String msg) {
        HiLog.error(label, msg);
    }
}
